﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using PenoyShop.Utilities;

namespace PeonyShop.DAL
{
    public class DACategory : BaseDA
    {
        public DataSet GetAllCategoryByParenṭ(int ParentId, bool IsActive, bool IsDelete)
        {
            DataSet ds = new DataSet();
            try
            {
                DbCommand cmd = DB.GetStoredProcCommand("sp_GetCateByParentId");
                DB.AddInParameter(cmd, "@ParentCateId", SqlDbType.Int, ParentId);
                DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, IsActive);
                DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, IsDelete);
                ds = DB.ExecuteDataSet(cmd);
                return ds;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("GetAllCategoryByParenṭ ", ex);
                return null;
            }
                
        }

        public DataSet GetCategoryByName(string sName, int IsActive, int IsDelete)
        {
            DataSet ds = new DataSet();
            try {
                DbCommand cmd = DB.GetStoredProcCommand("sp_GetCategorybyName");
                DB.AddInParameter(cmd, "@CateName", SqlDbType.NVarChar, sName);
                if(IsActive != -1)
                    DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, IsActive);
                if (IsDelete != -1)
                    DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, IsDelete);
                ds = DB.ExecuteDataSet(cmd);
                return ds;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("GetCategoryByName" ,ex);
                ds = null;
            }
            return ds;
        }




        public bool UpdateCate(int CateId, string Name, string Description, int Order, bool IsActive, bool IsDelete)
        {
            bool bResult = false;
            try
            {
                DbCommand cmd = DB.GetStoredProcCommand("sp_UpdateCategory");
                DB.AddInParameter(cmd, "@CateId", SqlDbType.Int,CateId);
                DB.AddInParameter(cmd, "@Name",SqlDbType.NVarChar,Name);
                DB.AddInParameter(cmd, "@Description", SqlDbType.NVarChar, Description);
                DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, IsActive);
                DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, IsDelete);
                DB.AddInParameter(cmd, "@OrderN", SqlDbType.Int, Order);

                DB.ExecuteNonQuery(cmd);
                bResult = true;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("UpdateCate ",ex);
                bResult = false;
            }
            return bResult;
        }

        public bool InsertCate(string Name, string DescrN, int ParentId, bool IsActive, bool IsDelete, int Order)
        {
            bool bResult = false;
            try
            {
                DbCommand cmd = DB.GetStoredProcCommand("sp_InsertCategory");
                DB.AddInParameter(cmd, "@Name", SqlDbType.NVarChar, Name);
                DB.AddInParameter(cmd, "@Description", SqlDbType.NVarChar, DescrN);
                DB.AddInParameter(cmd, "@ParentId", SqlDbType.Int, ParentId);
                DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, IsActive);
                DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, IsDelete);
                DB.AddInParameter(cmd, "@Order", SqlDbType.Int, Order);

                DB.ExecuteNonQuery(cmd);
                bResult = true;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("InsertCate ",ex);
                bResult = false;
            }
            return bResult;
        }

       
    }
}
